package mysql

import (
	"gorm.io/gorm"
	"shuqi-novel/shuqirpc/shuqirpc"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(20);not null"`
	Password string `gorm:"type:char(32);not null"`
}

func (u *User) LoginFind(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile = ?", mobile).First(u).Error
}

type Book struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20);not null"`
	Title   string `gorm:"type:varchar(20);not null"`
	Context string `gorm:"type:varchar(30);not null"`
	UserId  int    `gorm:"type:int;not null"`
}

func (b *Book) BookList(db *gorm.DB, name string) error {
	return db.Debug().Where("name = ?", name).First(b).Error
}

func (b *Book) Addbook(db *gorm.DB) error {
	return db.Debug().Create(&b).Error
}

func (b *Book) UpdateBook(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Updates(b).Error
}

func (b *Book) LsitBook(db *gorm.DB, list []*shuqirpc.GetList, in *shuqirpc.ListRequest) ([]*shuqirpc.GetList, error) {
	err2 := db.Debug().Model(&b).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
